<?php

class myLib {

    static public function slugify($text){
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
        $text = trim($text, '-');
        if (function_exists('iconv')){
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }
        $text = strtolower($text);
        $text = preg_replace('~[^-\w]+~', '', $text);
        return $text;
    }

    function getMes($date){
        $mes = substr($date,5,2);
        switch ($mes){
            case 1: $date = "Enero";break;
            case 2: $date = "Febrero";break;
            case 3: $date = "Marzo";break;
            case 4: $date = "Abril";break;
            case 5: $date = "Mayo";break;
            case 6: $date = "Junio";break;
            case 7: $date = "Julio";break;
            case 8: $date = "Agosto";break;
            case 9: $date = "Setiembre";break;
            case 10: $date = "Octubre";break;
            case 11: $date = "Noviembre";break;
            case 12: $date = "Diciembre";break;
        }
        return $date;
    }

    function getDay($date, $lang = 'es'){
        $year = substr($date,0,4);
        $month = substr($date,5,2);
        $day = substr($date,8,2);

        // 1 (para lunes) hasta 7 (para domingo)
        $number_day = date('N',mktime(0,0,0,$month,$day,$year));
        $number_day = intval($number_day);
        switch($lang){
            case 'es':
                $lang = array('Ninguno','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo');
                break;
            case 'en':
                $lang = array('None','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
                break;
        }
        return $lang[$number_day];
    }

    function getMonth($date, $lang = 'es'){
        $month = substr($date,5,2);
        $month = intval($month);
        switch($lang){
            case 'es':
                $lang = array('Ninguno','Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Setiembre','Octubre','Noviembre','Diciembre');
                break;
            case 'en':
                $lang = array('None','January','February','March','April','May','June','July','August','September','October','November','December');
                break;
        }
        return $lang[$month];
    }

    function getOrdinal($date){
        $year = substr($date,0,4);
        $month = substr($date,5,2);
        $day = substr($date,8,2);

        $ordinal_day = date('S',mktime(0,0,0,$month,$day,$year));
        return $ordinal_day;
    }

    function formattedDate($date){
        $today = date('Y-m-d');
        $day_ago = date('Y-m-d', mktime(date("H"), date("i"), date("s"), date("m"), date("d")-1, date("Y")));
        if(substr($date,0,10) == $today){
            $f_date = "Hoy a las ";
        }elseif(substr($date,0,10) == $day_ago){
            $f_date = "Ayer a las ";
        }elseif(substr($date,0,10) < $day_ago){
            $f_date = substr($date,8,2)." de ".$this->getMes($date)." a las ";
        }
        $f_date .= substr($date,11,8);
        return $f_date;
    }

    function formattedDate2($date){
        $ano = substr($date,0,4);
        $mes = substr($date,5,2);
        $dia = substr($date,8,2);

        $det_mes = $this->getMes($date);

        $f_date = $this->getDay($date).", ".$dia." de ".$det_mes." del ".$ano;
        return $f_date;
    }

    function formattedDate3($date){
        $ano = substr($date,0,4);
        $mes = substr($date,5,2);
        $dia = substr($date,8,2);

        $det_mes = $this->getMes($date);

        $f_date = $dia."-".$mes."-".$ano;
        return $f_date;
    }

    function formattedDateAbbr($date, $lang='es'){
        $ano = substr($date,0,4);
        $mes = substr($date,5,2);
        $dia = substr($date,8,2);
        switch($lang){
            case 'es':
                $f_date = $dia.' '.substr($this->getMonth($date,'es'),0,3);
                break;
            case 'en':
                $f_date = substr($this->getMonth($date,'en'),0,3).' '.$dia.$this->getOrdinal($date);
                break;
        }
        return $f_date;
    }

    function getUsersFromPractil($ids = array()){
        $users = array();

        $myPractil = new myPractil();
        $url = $myPractil->url_practil_get_users($ids);

        $fgc = @file_get_contents($url);
        if($fgc === FALSE){
            sfContext::getInstance()->getUser()->setFlash('message','Not connected to Practil');
        }else{
            $json_get_users = json_decode($fgc);
            if($json_get_users->{'success'}==false){
                sfContext::getInstance()->getUser()->setFlash('message',$json_get_users->{'message'});
            }else{
                $users = $json_get_users->{'practil'}->{'accounts'};
                if(count($users)==0){
                    sfContext::getInstance()->getUser()->setFlash('message','Users Array returned 0 elements');
                }
            }
        }

        return $users;
    }

    function getUserIdsFromPractilByName($string=""){
        $user_ids = array();
        $practil_accounts = array();

        $myPractil = new myPractil();
        $url = $myPractil->url_practil_get_users_by_name_like($string);

        $fgc = @file_get_contents($url);
        if($fgc === FALSE){
            sfContext::getInstance()->getUser()->setFlash('message','Not connected to Practil');
        }else{
            $json_get_users = json_decode($fgc);
            if($json_get_users->{'success'}==false){
                sfContext::getInstance()->getUser()->setFlash('message',$json_get_users->{'message'});
            }else{
                $practil_accounts = $json_get_users->{'practil'}->{'accounts'};
            }
        }
        foreach($practil_accounts as $pa){
            $user_ids[] = $pa->{'id'};
        }

        return $user_ids;
    }

}
?>
